package action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import model.Department;
import model.User;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import service.DeptService;
import service.UserService;
import action.base.BaseAction;

import com.github.miemiedev.mybatis.paginator.domain.PageList;

/**
 * 用户管理
 * 
 * @author yakun0622
 * 
 */
@Controller
@Scope("prototype")
public class UserAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 
	 */
	@Autowired
	private UserService userService;
	@Autowired
	private DeptService deptService;
	private User user;
	private User user_self;
	private PageList<HashMap<String, Object>> pageList;
	private int total;
	private Boolean success = false;
	private List<Integer> roleidList;
	private HashMap<String, Object> hashMap = new HashMap<String, Object>();
	// 接收客户端传来的验证码
	private String securityCode;
	private boolean unique = false;

	/**
	 * 登陆时加载
	 * 
	 * @return
	 */
	public String load() {
		securityCode = super.getParameterStr("securityCode");
		Boolean security = true;
		/* 验证码 */
		if (!securityCode.equals(super.getSession()
				.get("SESSION_SECURITY_CODE"))) {
			security = false;
			hashMap.put("security", security);
		} else {
			hashMap.put("security", security);
			user = userService.load(user);
			if (user != null && user.getId() != null) {
				super.getSession().put("user", user);
				hashMap.put("user", user);
			}
		}
		setSuccess(true);
		return "loginSecurity";
	}

	/**
	 * 用户名唯一
	 * 
	 * @return
	 */
	public String uniqueName() {
		String name = super.getParameterStr("name");
		String id = super.getParameterStr("uniqueId");
		hashMap.put("username", name);
		hashMap.put("id", id);
		user = userService.uniqueName(hashMap);
		if (user == null) { /* 查出结果为不存在该用户名 */
			unique = true;
		}
		return "unique";
	}

	/**
	 * grid 显示
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String queryUserPage() {
		int page = super.getParameterInt("page");
		int limit = super.getParameterInt("limit");
		String deptId = super.getParameterStr("deptId");
		HashMap<String, Object> map = new HashMap<String, Object>();
		@SuppressWarnings("rawtypes")
		ArrayList list = new ArrayList();
		if (StringUtils.isNotBlank(deptId)) {
			list.add(Integer.parseInt(deptId));
			List<Department> getByPid = deptService.getTreeBypid(Integer
					.parseInt(deptId));
			if (getByPid.size() != 0) {
				for (int i = 0; i < getByPid.size(); i++) {
					// deptIds[i] = getByPid.get(i).getId();
					list.add(getByPid.get(i).getId());
				}
			}
		}
		if (list.size() == 0 || deptId.equals("1")) {
			list = null;
		}
		map.put("deptIds", list);
		pageList = userService.queryUserPage(page, limit, map);
		setTotal(pageList.getPaginator().getTotalCount());
		return "queryUserPage";
	}

	/**
	 * 新增 或者 修改 user
	 * 
	 * @return
	 */
	public String saveOrUpdate() {
		String flag = super.getParameterStr("flag");
		if (flag != null) {
			if (flag == "personal") {
				User currentUser = super.getUser();
				currentUser.setName(user_self.getName());
				currentUser.setSex(user_self.getSex());
				currentUser.setBirthday(user_self.getBirthday());
				currentUser.setTel(user_self.getTel());
				currentUser.setMobilephone(user_self.getMobilephone());
				currentUser.setQq(user_self.getQq());
				currentUser.setEmail(user_self.getEmail());
				currentUser.setFax(user_self.getFax());
				currentUser.setDescription(user_self.getDescription());
				user = currentUser;
			}
		}
		userService.saveOrUpdate(user, flag);
		success = true;
		return "success";
	}

	/**
	 * 根据用户id拿到角色
	 * 
	 * @return
	 */
	public String queryRoleIds() {
		Integer userid = super.getParameterInt("userid");
		roleidList = userService.queryRoleIds(userid);
		success = true;
		return "queryRoleIds";
	}

	public String getById() {
		int id = super.getParameterInt("id");
		user = userService.getById(id);
		setSuccess(true);
		return "user";
	}

	public String getCurUser() {
		user = super.getUser();
		hashMap = userService.getCurUser(user.getId());
		setSuccess(true);
		return "hashMap";
	}

	public String delete() {
		String ids = super.getParameterStr("ids");
		userService.delete(ids);
		setSuccess(true);
		return "success";
	}

	public String changePsw() {
		String oldpsw = super.getParameterStr("oldPassword");
		String newpsw = super.getParameterStr("newPassword");
		String flag = super.getParameterStr("flag");
		User user = super.getUser();
		if (oldpsw.equals(user.getPassword())) {
			user.setPassword(newpsw);
			userService.saveOrUpdate(user, flag);
			setSuccess(true);
		} else {
			setSuccess(false);
		}
		return "success";
	}

	public Boolean getSuccess() {
		return success;
	}

	public void setSuccess(Boolean success) {
		this.success = success;
	}

	public PageList<HashMap<String, Object>> getPageList() {
		return pageList;
	}

	public void setPageList(PageList<HashMap<String, Object>> pageList) {
		this.pageList = pageList;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<Integer> getRoleidList() {
		return roleidList;
	}

	public void setRoleidList(List<Integer> roleidList) {
		this.roleidList = roleidList;
	}

	public HashMap<String, Object> getHashMap() {
		return hashMap;
	}

	public void setHashMap(HashMap<String, Object> hashMap) {
		this.hashMap = hashMap;
	}

	public String getSecurityCode() {
		return securityCode;
	}

	public void setSecurityCode(String securityCode) {
		this.securityCode = securityCode;
	}

	public boolean isUnique() {
		return unique;
	}

	public void setUnique(boolean unique) {
		this.unique = unique;
	}

	public User getUser_self() {
		return user_self;
	}

	public void setUser_self(User user_self) {
		this.user_self = user_self;
	}

}
